#!/user/bin/env python
# -*- coding:utf-8 -*-
#/home/zc/workspace/tinyfeet/trunk/src/model/updateweibo.py

'''
Created on 2011-9-19

@author: zc
'''
from weibo import APIClient
from config.settings import CONSUME_KEY, CONSUME_SECRET, PRE_URL, _CALLBACK_URL
import web
import simplejson
from controllers.sessionHandler import session_get

def inviteFriend( uname ):
    """发送微博邀请好友使用tinyfeet
    微博形式为@形式,可以邀请多个好友
    
    参数：被邀请人微博ID
    """
    content = "我在用Tinyfeet记录脚印，还能回放我de脚印记录， %s 快来试试。点这--> http://tinyfeet.us" % ( uname )
    annotations = "{tinyfeet:invite}"
    res = updatetweet( content, annotations )
    return res


def tfeetweet( content, comment = None, uname , lat = None, long = None, pic = None, weibo_id = None ):
    """tfeet发/转发/评论到微博
    参数：lat,long,content,annotations
    """
    annotations = "{tinyfeet:tfeetweet}"
    res = None
    cmt_text = comment if comment is not None else ' '
    if weibo_id is not None: #转发该微博
        res = repost( weibo_id , cmt_text )
    else:
        status = cmt_text + "//@" + uname + ":" + content
        annotations = "{tinyfeet:comment}"  
        if pic:
            res = updatetweet( status, annotations, lat, long )##不带图片
        else:
            res = uploadtweet( status, annotations, PRE_URL + pic, lat, long )##带图片
    return res


def broadcastfeet():
    """ 发微博通知大家我在用tinyfeet
    """
    content = "我在用Tinyfeet记录脚印，还能回放我de脚印记录，确实有点意思，试试点这--> http://tinyfeet.us" 
    annotations = "{tinyfeet:broadcastfeet}"
    res = updatetweet( content, annotations )
    return res


def updatetweet( as_content, as_annotations , as_lat = '0.0', as_long = '0.0' ):
    """ 发微博
    as_connent 微博正文
    as_annotations 项目统计用信息  
    """
    res = None
    client = weibo_API_init( web.config._session )
    annotations_info = simplejson.dumps( as_annotations )
    try:
        res = client.post.statuses__update( status = as_content , lat = as_lat, long = as_long, annotations = annotations_info )
        res = '{"msg":"200","weburl":"http://api.t.sina.com.cn/%s/statuses/%s"}' % ( res.user.id, res.id )
    except:
        res = '{"msg":"' + res.error_code + '", "errmsg":"' + res.error + '"}'
    return res
        
    
def uploadtweet( as_content, as_annotations, as_pic , as_lat = '0.0', as_long = '0.0' ):
    """ 发微博带图片
    as_connent 微博正文
    as_pic  图片 
    """
    res = None
    client = weibo_API_init( web.config._session )
    annotations_info = simplejson.dumps( as_annotations )
    try:
        f = open( as_pic )
        res = client.post.statuses__upload( status = as_content , pic = f, lat = as_lat, long = as_long, annotations = annotations_info )
        res = '{"msg":"200","weburl":"http://api.t.sina.com.cn/%s/statuses/%s"}' % ( res.user.id, res.id )
    except:
        res = '{"msg":"' + res.error_code + '", "errmsg":"' + res.error + '"}'
    finally:
        f.close()
    return res
    
    
def addcomments( weibo_id, text ):
    """ 发布一条评论"""
    res = None
    client = weibo_API_init( web.config._session )
    try:
        res = client.post.comments__create( comment = text , id = weibo_id )
        res = '{"msg":"200","weburl":"http://api.t.sina.com.cn/%s/statuses/%s"}' % ( res.user.id, res.status.id )
    except:
        res = '{"msg":"' + res.error_code + '", "errmsg":"' + res.error + '"}'
    return res


def repost( weibo_id, status ):
    """转发微博 """
    res = None
    client = weibo_API_init( web.config._session )
    try:
        res = client.post.statuses__repost( status = status , id = weibo_id )
        res = '{"msg":"200","weburl":"http://api.t.sina.com.cn/%s/statuses/%s"}' % ( res.user.id, res.id )
    except:
        res = '{"msg":"' + res.error_code + '", "errmsg":"' + res.error + '"}'
    return res

    
def weibo_API_init( session ):
    access_token, uid, expires_in = session_get( session )
    client = APIClient( app_key = CONSUME_KEY, app_secret = CONSUME_SECRET )
    if access_token is None:
        raise web.found( client.get_authorize_url( _CALLBACK_URL ) )
    client.set_access_token( access_token, expires_in )
    return client 


if __name__ == '__main__':
    pass
